<template>
    <div class="shopCart">
        <div class="title">购物车</div>
        <div class="clear">
            <button @click="clear()">删除</button>

        </div>
        <div class="goodsCard" v-for="(item,index) of cart" :key="index">
            <input type="checkbox" name="" id="" @click="show($event,index)">
            <img :src="item.goods.image">
            <div class="name"> 
                <p class="tip">{{item.goods.name}}</p>
                <div class="stepper">
                    <span @click="reduce(item.goods.id)">-</span>
                    <span>{{item.count}}</span>
                    <span @click="add(item.goods.id)">+</span>   
                </div>
            </div>
            <div class="price">
                <p>￥{{item.goods.price}}</p>
                <p>x{{item.count}}</p>
                <p>￥{{item.goods.total}}</p>
            </div>
        </div>
            <div class="total">商品总价:￥{{total}}</div>
    </div>
</template>

<script>
    export default {
        data(){
            return {
                cart:{},
                total:0,
                isTure:""
            }
        },
        methods:{
            add(ID){
                this.$store.dispatch("addNum",{id:ID})
                this.updata()
            },
            reduce(ID){
                this.$store.dispatch("reduceNum",{id:ID})
                this.updata()
            },
            clear(){
                        this.$store.dispatch("allClear")
                        this.updata()
            },
            updata(){
                this.cart = JSON.parse(localStorage.getItem("cart"))
                this.total = 0
                for(let i in this.cart){
                 this.total += this.cart[i].goods.total
                //  Math.floor(parseInt(this.total))
                }
                //  console.log(Math.floor(parseInt(this.total)))
            },
            show(e,idx){
                this.cart[idx].goods.isTrue = e.target.checked
                localStorage.setItem("cart",JSON.stringify(this.cart))
                // console.log(this.cart[idx].goods.isTrue)
            }
        },
        created(){
            this.updata()
            // localStorage.clear()
        },
  }
</script>

<style lang="scss" scoped>
$border:1px solid #eee;
@mixin list {
  height: 50px;
  line-height: 50px;
  text-align: center;
  font-size: 18px;
  font-weight: 550;
  border-bottom: 1px solid #eeeeee;
}
    .shopCart{
        .title{
          background-color: white;
          @include list
        }
        .clear{
            height: 60px;
            // line-height: 60px;
            border-bottom: $border;
            background-color: white;
            text-align: right;
            padding: 10px;
            button{
                background-color: #ff4444;
                padding: 10px;
                color: #ffffff;
                border-radius: 5px;
            }
        }
        .goodsCard{
            display: flex;
            background-color: white;
            border-bottom: $border;
            font-size: 14px;
            input{
                margin-top: 45px;
            }
            img{
                width: 70px;
                height: 90px;
                margin: 10px;
            }
            .name{
                // vertical-align: middle;
                display: flex;
                flex-direction: column;
                .tip{
                    margin-top: 10px;
                    width: 220px;
                    height: 50px;
                }
                .stepper{
                    margin: 10px 0;
                    span{
                        display: inline-block;
                        width: 40px;
                        height: 30px;
                        border:1px solid #ccc;
                        text-align: center;
                        line-height: 30px;
                    }
                }
            }
            .price{
                margin-top: 10px;
                width: 50px;
                text-align: right;
            }

        }
        .total{
            font-size: 18px;
            text-align: right;
            padding: 10px;
            border-bottom: $border;
            background-color: white;
        }
    }
</style>